<?php

App::uses('AdminController', 'Controller');

class AdminPagesController extends AdminController {
    
    public $uses = array('Page');
    
    public function index() {
        $this->set('pages', $this->Page->find('all'));
    }
    
    public function create() {
        $this->set('data', $this->Page->create());
        $this->render('form');
    }
    
    public function edit($id) {
        $this->set('data', $this->Page->findById($id));
        $this->render('form');
    }
    
    public function save() {
        $data = $this->data;
        if (! empty($data)) {
            $this->Page->create($data);
            
            if (! $this->Page->validates()) {
                $this->setFlash('Uneti podaci nisu validni!');
                $this->set('data', $data);
                return $this->render('form');
            }
            
            if (! $this->Page->save($data)) {
                $this->setFlash('Greška pri čuvanju podataka!');
                $this->set('data', $data);
                return $this->render('form');
            }
            
            $this->setFlash('Stranica je uspešno sačuvana', 'ok');
            $this->redirect('edit/' . $this->Page->id);
        }
        
        $this->redirect('index');
    }
    
    public function delete($id) {
        if ($this->Page->delete($id)) {
            $this->setFlash('Stranica je uspešno obrisana.', 'ok');
        } else {
            $this->setFlash('Greška pri brisanju stranice!');
        }
        $this->redirect('index');
    }
    
}